package com.esiag.pdsteam.dao;

import java.io.Serializable;
import java.lang.reflect.ParameterizedType;
import java.util.List;

import javax.persistence.EntityManager;



public class GenericSpringDao<T, ID extends Serializable> implements GenericDao<T, ID> {

	protected EntityManager manager = null;

	private Class<T> persistentClass;

	@SuppressWarnings("unchecked")
	public GenericSpringDao() {
		this.persistentClass = (Class<T>) ((ParameterizedType) getClass()
				.getGenericSuperclass()).getActualTypeArguments()[0];
	}

	public EntityManager getManager() {
		return manager;
	}

	public void setManager(EntityManager manager) {
		this.manager = manager;
	}
	
	public T findById(ID id) {
		T entity = (T) manager.find(persistentClass, id);
		return entity;
	}

	@SuppressWarnings("unchecked")
	public List<T> findAll() {
		return manager.createQuery("from " + persistentClass.getName()).getResultList();
	}
	
}

